package com.clstu.exit_;

public class ThreadExit {

    public static void main(String[] args) throws InterruptedException {//

        // 通过主线程修改变量通知子线程退出
        Ha ha = new Ha();
        new Thread(ha).start();

        Thread.sleep(5*1000);

        ha.setLoop(false);//使子线程跳出循环

        System.out.println("吉时已到，程序退出。。。。");

    }

}
class Ha implements Runnable{
    private int times = 0;
    private boolean loop = true;

    public void setLoop(boolean loop) {
        this.loop = loop;
    }

    @Override
    public void run() {
        while (loop) {
            System.out.println("Ha is running....."+(++times));
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}